package cn.itsource.hrm.Thread;

import cn.itsource.hrm.domain.User;
import cn.itsource.hrm.service.IUserService;
import cn.itsource.hrm.utils.MyTools;
import cn.itsource.hrm.utils.excel.ReadExcelTools;

import java.util.List;
import java.util.concurrent.CountDownLatch;

//线程类：添加Excel中的数据到数据库的线程
public class AddUserRunnable implements Runnable {

    private String excelPath;
    private IUserService userService;
    private CountDownLatch cdl;

    //构造器注入
    public AddUserRunnable(String excelPath, IUserService userService, CountDownLatch cdl) {
        this.excelPath = excelPath;
        this.userService = userService;
        this.cdl = cdl;
    }

    //线程体：做任务的地方
    @Override
    public void run() {
        //开始读取excel内容
        List<User> userList = ReadExcelTools.readExcel(excelPath);
        //写到DB
        userService.insertBatch(userList);
        //删除文件
        MyTools.deleteFile(excelPath);
        //扣减
        cdl.countDown();
        System.out.println("线程【" + Thread.currentThread().getName() + "】任务完成");
    }
}